package com.dchh.android.suverynew.http;

import com.dchh.android.suverynew.BuildConfig;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;

/**
 * Created by eric on 2016/7/5.
 */
public class RetrofitManager {
    private static RetrofitManager mInstance;
    private Retrofit mRetrofit;
    //okhttp 拦截器，用于配置
    private static HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .retryOnConnectionFailure(true)
            .connectTimeout(BuildConfig.TIMEOUT_SECONDS, TimeUnit.SECONDS)
            .addInterceptor(loggingInterceptor)
            .build();

    static {
        //日志，可以配置 level 为 BASIC / HEADERS / BODY
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    }

    private RetrofitManager() {
        //retrofit 配置
        mRetrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.BASE_URL)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(SimpleXmlConverterFactory.create())
                .addConverterFactory(FastJsonConverterFactory.create())
                .build();
    }

    public Retrofit getRetrofit() {
        return mRetrofit;
    }

    public static RetrofitManager newInstance() {

        if (mInstance == null) {
            synchronized (RetrofitManager.class) {
                mInstance = new RetrofitManager();
            }
        }
        return mInstance;
    }
}
